<?php


namespace backend\models;

use common\models\User;
use Yii;
use yii\base\Model;

class ManagerForm extends Model
{
    public $username;
    public $password;


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required','message' => '不能为空.'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '已存在.'],
            ['username', 'string', 'min' => 2, 'max' => 255],
            ['password', 'required','message' => '不能为空.'],
            ['password', 'string'],
        ];
    }

    /**
     * 增加管理员
     *
     * @return bool whether the creating new account was successful and email was sent
     */
    public function add()
    {
        $user = new User();
        $user->username = $this->username;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->generateEmailVerificationToken();
        return $user->save() ;
    }

}